package com.simple.util.base;

/**
 * @program: simple_tools
 * @description: 编码转换工具类 unicode与utf-8 互相转换
 * @author: Mr.chen
 * @create: 2020-05-18 11:05
 **/
public class CodeConvertUtil {

    /**
     * utf-8 转unicode
     *
     * @param utf8Str
     * @return String
     */
    public static String utf8ToUnicode(String utf8Str) {
        char[] charBuf = utf8Str.toCharArray();

        StringBuffer unicodeBuf = new StringBuffer();
        for (int i = 0; i < utf8Str.length(); i++) {
            Character.UnicodeBlock ub = Character.UnicodeBlock.of(charBuf[i]);
            if (ub == Character.UnicodeBlock.BASIC_LATIN) {
                unicodeBuf.append(charBuf[i]);
            } else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                int j = charBuf[i] - 65248;
                unicodeBuf.append((char) j);
            } else {
                short s = (short) charBuf[i];
                String hexS = Integer.toHexString(s & 0xffff);
                String unicode = "\\u" + hexS;
                unicodeBuf.append(unicode.toLowerCase());
            }
        }
        return unicodeBuf.toString();
    }

    /**
     *
     * @param unicodeStr
     * @return String
     */
    public static String unicodeToUtf8(String unicodeStr) {
        char aChar;
        int len = unicodeStr.length();
        StringBuffer utf8Buf = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = unicodeStr.charAt(x++);
            if (aChar == '\\') {
                aChar = unicodeStr.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = unicodeStr.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = ((value << 4) + aChar) - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = ((value << 4) + 10 + aChar) - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = ((value << 4) + 10 + aChar) - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
                        }
                    }
                    utf8Buf.append((char) value);
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    }
                    utf8Buf.append(aChar);
                }
            } else {
                utf8Buf.append(aChar);
            }
        }
        return utf8Buf.toString();
    }

    public static void main(String[] args) {
        String src = "中华人民共和国的荣耀32323";
        String unicodeStr = utf8ToUnicode(src);
        System.out.println(unicodeStr);

        String utf8Str = unicodeToUtf8("version=[School(3.1.2_31212)/iPhone(8.1.2)];screen=[w:320,h:568];info=[identify:cn.ipipa.school];model=[\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd\u7684\u8363\u800032323]");
        System.out.println(utf8Str);
    }
}
